/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormDaftarOrderPenjualan.java
 *
 * Created on Apr 6, 2011, 11:28:41 PM
 */

package com.kifas.sales.ui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;



import com.kifas.model.xml.PenjualanDetailModel;
import com.kifas.model.xml.PenjualanModel;
import com.kifas.service.MasterService;
import com.kifas.service.PenjualanService;

/**
 *
 * @author dhamarsu
 */
public class FormDaftarPenjualanJthTempo extends javax.swing.JInternalFrame {
    private MasterService masterService = new MasterService();
    private DateFormat df = new SimpleDateFormat("ddMMyyyy");

    private PenjualanModel penjualanModel;
    private List<PenjualanModel> salesList;
    private PenjualanService penjualanService = new PenjualanService();
    private List<PenjualanDetailModel> salesDetailList;    
    
    /** Creates new form FormDaftarOrderPenjualan */
    public FormDaftarPenjualanJthTempo() {
        initComponents();
        tablePenjualan.setAutoCreateColumnsFromModel(false);
        tablePenjualan.getSelectionModel().addListSelectionListener(new SalesSelectionListener());
        refreshTable();

        tableDetailSales.setAutoCreateColumnsFromModel(false);
        salesDetailList = new ArrayList<PenjualanDetailModel>();
        tableDetailSales.setModel(new PenjualanDetailTableModel(salesDetailList));
    }

    private void refreshTable(){
        salesList = penjualanService.getPenjualansJthTempo(df.format(new Date()));
        tablePenjualan.setModel(new PenjualanTableModel(salesList));
    }
    private class SalesSelectionListener implements ListSelectionListener{

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if(tablePenjualan.getSelectedRow()>=0){
                penjualanModel = salesList.get(tablePenjualan.getSelectedRow());
                salesDetailList = new ArrayList<PenjualanDetailModel>(penjualanModel.getPenjualanDetails());
                tableDetailSales.setModel(new PenjualanDetailTableModel(salesDetailList));
                tabPanel.setSelectedIndex(1);
            }
        }

    }


    private class PenjualanTableModel extends AbstractTableModel{

        private List<PenjualanModel> salesList;

        public PenjualanTableModel(List<PenjualanModel> salesList) {
            this.salesList = salesList;
        }

        public int getRowCount() {
            return salesList.size();
        }

        public int getColumnCount() {
            return 3;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            PenjualanModel s = salesList.get(rowIndex);
            switch(columnIndex){
                case 0: return s.getNoPenjualan();
                case 1: return s.getCustomer().getNama();
                case 2: return s.getSales().getNama();
                case 3: return new SimpleDateFormat("dd/mm/yyyy").format(s.getTanggal());
                case 4: return new SimpleDateFormat("dd/mm/yyyy").format(s.getTanggalKirim());
                case 5: return new SimpleDateFormat("dd/mm/yyyy").format(s.getTanggalJatuhTempo());
                case 6: return s.getTotal();
                default: return "";
            }
        }
    }


    private class PenjualanDetailTableModel extends AbstractTableModel{

        private List<PenjualanDetailModel> salesDetailList;

        public PenjualanDetailTableModel(List<PenjualanDetailModel> salesDetailList) {
            this.salesDetailList = salesDetailList;
        }

        public int getRowCount() {
            return salesDetailList.size();
        }

        public int getColumnCount() {
            return 3;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            PenjualanDetailModel s = salesDetailList.get(rowIndex);
            switch(columnIndex){
                case 0: return s.getBarang().getKdBarang();
                case 1: return s.getBarang().getNamaBarang();
                case 2: return s.getBulanKadaluarsa();
                case 3: return s.getGudang().getNamaGudang();
                case 4: return s.getBarang().getSatuan().getSatuan();
                case 5: return s.getQty();
                case 7: return s.getSubTotal();
                case 6: return s.getPrice();
                default: return "";
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        tabPanel = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablePenjualan = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableDetailSales = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        jButton4 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Daftar Order Penjualan");

        tabPanel.setName("tabPanel"); // NOI18N

        jPanel3.setName("jPanel3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tablePenjualan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "No PO", "Customer", "Sales", "Tanggal", "Tanggal Kirim", "Tanggal Jt Tempo", "Total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablePenjualan.setName("tablePenjualan"); // NOI18N
        jScrollPane1.setViewportView(tablePenjualan);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 974, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
        );

        tabPanel.addTab("Daftar Penjualan", jPanel3);

        jPanel5.setName("jPanel5"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tableDetailSales.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Kd Barang", "Nama", "Kadaluarsa", "Gudang", "Satuan", "Qty", "Harga", "Sub Total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tableDetailSales.setName("tableDetailSales"); // NOI18N
        jScrollPane2.setViewportView(tableDetailSales);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 974, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabPanel.addTab("Detail Penjualan", jPanel5);

        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        jButton4.setText("Refresh");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton4);

        jButton1.setText("Create DO");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 1006, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 982, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        refreshTable();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (penjualanModel != null){
            FormPengiriman formPengiriman = new FormPengiriman(penjualanModel);
            this.getDesktopPane().add(formPengiriman);
            formPengiriman.setVisible(true);
        }
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTabbedPane tabPanel;
    private javax.swing.JTable tableDetailSales;
    private javax.swing.JTable tablePenjualan;
    // End of variables declaration//GEN-END:variables

}
